const querystring = require("querystring");
const http = require("http");

const serve = http.createServer();
serve.on("request", (req, res) => {
  const myUrl = new URL(req.url, "http://127.0.0.1:3000?a=1");
  res.setHeader("Content-Type", "text/html;charset=utf-8");

  const data = myUrl.search;
  console.log("拿到查询字符串   " + data);
  const newData = data.substr("1", data.length);
  console.log("截取字符串  " + newData);

  const obj = querystring.parse(newData);
  console.log("把查询字符串转为对象");
  console.log(obj);

  console.log("把对象转为查询字符串   " + querystring.stringify(obj));
  // const obj = querystring.parse(newData);
  // for (var k in obj) {
  //     console.log(k + ' ' + obj[k]);
  // }
  res.end("请求成功");
});
serve.listen(3000, function () {
  console.log("server running at http://127.0.0.1:3000");
});
